import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
const router = new VueRouter({
  mode: "hash",
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/home",
      name: "home",
      redirect: "/welcome",
      component: () => import("@/views/first-level/Home.vue"),
      children: [
        {
          path: "/welcome",
          name: "welcome",
          component: () => import("@/views/first-level/Welcome.vue"),
        },
        {
          path: "/users",
          name: "users",
          component: () => import("@/views/second-level/Users.vue")
        },
        {
          path:'/roles',
          name:'roles',
          component:()=>import('@/views/second-level/Roles.vue')
        },
        {
          path:'/rights',
          name:'rights',
          component:()=>import('@/views/second-level/Rights.vue')
        },
        {
          path:'/goods',
          name:'goods',
          component:()=>import('@/views/second-level/Goods.vue')
        },
        {
          path:'/params',
          name:'params',
          component:()=>import('@/views/second-level/Params.vue')
        },
        {
          path:'/categories',
          name:'categories',
          component:()=>import('@/views/second-level/Categories.vue')
        },
        {
          path:'/orders',
          name:'orders',
          component:()=>import('@/views/second-level/Orders.vue')
        },
        {
          path:'/reports',
          name:'reports',
          component:()=>import('@/views/second-level/Reports.vue')
        },
        {
          path:'/addgoods',
          name:'addgoods',
          component:()=>import('@/views/second-level/AddGoodsInfo.vue')
        }
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/first-level/Login.vue"),
    },
    {
      path:'/mockdemo',
      name:'mockdemo',
      component:()=>import('@/views/first-level/MockDemo.vue')
    },
    {
      path:'/feige',
      name:'/feige',
      component:()=>import('@/views/first-level/Feige.vue')
    }
  ],
});
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    //如果要去的路径是登录
    return next(); //放行 并且下面代码不执行
  }
  if (!window.sessionStorage.getItem("token")) {
    //如果查询不到token
    return next("/login"); //只能去登录页面
  }
  next();
});
export default router;
